//
//  GKZYPostBarImageViewController.m
//  GKZYAid
//
//  Created by lanou3g on 16/3/14.
//  Copyright © 2016年 GKZYAid. All rights reserved.
//

#import "GKZYPostBarImageViewController.h"
#define CELLID @"cell_id"

@interface GKZYPostBarImageViewController ()

@end

@implementation GKZYPostBarImageViewController

//加载数据
- (void)viewWillAppear:(BOOL)animated
{
    if (self.arrayImage.count) {
        [self.arrayImage removeAllObjects];
    }
    AVQuery *query = [PostBar query];
    [query whereKey:@"strPublicationID" equalTo:self.strPublicationID];
    [query includeKey:@"arrayPublicationImage"];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        for (PostBar *bar in objects) {
            if (bar.arrayPublicationImage.count) {
                [self.arrayImage addObjectsFromArray:bar.arrayPublicationImage];
            }
        }
        [self.collectionView reloadData];
    }];
}

- (NSMutableArray *)arrayImage
{
    if (!_arrayImage) {
        _arrayImage = [[NSMutableArray alloc] init];
    }
    return _arrayImage;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UICollectionViewFlowLayout *layout  = [[UICollectionViewFlowLayout alloc] init];
    
    layout.itemSize = CGSizeMake(self.view.frame.size.width * 0.25, self.view.frame.size.width * 0.25);
    
    layout.sectionInset = UIEdgeInsetsMake(self.view.frame.size.width * 0.02, self.view.frame.size.width * 0.02, self.view.frame.size.width * 0.02, self.view.frame.size.width * 0.02);
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(self.view.frame.size.width * 0.02, 0, self.view.frame.size.width * 0.96, self.view.frame.size.height) collectionViewLayout:layout];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.collectionView];
    
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    
    [self.collectionView registerClass:[GKZYPublishCollectionViewCell class] forCellWithReuseIdentifier:CELLID];
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.arrayImage.count - 1;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    GKZYPublishCollectionViewCell *cellPublish = [collectionView dequeueReusableCellWithReuseIdentifier:CELLID forIndexPath:indexPath];
    if (indexPath.row < self.arrayImage.count - 1) {
        AVFile *fileImage = self.arrayImage[indexPath.row];
        [cellPublish.imageV sd_setImageWithURL:[NSURL URLWithString:fileImage.url]];
    }
    return cellPublish;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    GKZYPostBarImageShowViewController *postBarImageShowVC = [[GKZYPostBarImageShowViewController alloc] init];
    postBarImageShowVC.index = indexPath.row;
    postBarImageShowVC.arrayAllImages = self.arrayImage;
    [self.navigationController pushViewController:postBarImageShowVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
